<?php
namespace app\admin\controller;

use catetree\Catetree;
use think\Controller;

class Goods extends Controller
{
    public function lst()
    {

        $join = [
            ['category c', 'g.category_id=c.id'],
            ['brand b', 'g.brand_id=b.id', 'LEFT'],
            ['type t', 'g.type_id=t.id', 'LEFT'],
            ['product p', 'g.id=p.goods_id', 'LEFT'],
        ];
        $goodsRes = db('goods')->field('g.*,cate_name,brand_name,type_name,SUM(p.goods_number) gn')->alias('g')->join($join)->group('g.id')->order('g.id desc')->paginate(10);
        // print_r($goodsRes);
        $this->assign([
            'goodsRes' => $goodsRes,

        ]);
        return view('list');
    }
    public function add()
    {
        $attrRes = db('attr')->where(array('type_id' => 3))->select();

        if (request()->isPost()) {

            $data = input('post.');

            //验证
            $validate = validate('goods');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $add = model('goods')->save($data);

            if ($add) {

                $this->success('添加商品成功 ！', 'lst');
            } else {

                $this->error('添加商品失败');
            }
            return;
        }
        //会员级别数据
        $mlRes = db('member_level')->field('level_name,id')->select();
        //获取类型
        $typeRes = db('type')->select();
        //品牌分类
        $brandRes = db('brand')->field('id,brand_name')->select();
        //商品推荐位
        $goodsRecposRes = db('recpos')->where('rec_type', '=', 1)->select();

        //商品分类
        $category    = new Catetree();
        $categoryObj = db('category');
        $categoryRes = $categoryObj->order('sort ACS')->select();
        $categoryRes = $category->Catetree($categoryRes);
        $this->assign([
            'mlRes'          => $mlRes,
            'typeRes'        => $typeRes,
            'brandRes'       => $brandRes,
            'categoryRes'    => $categoryRes,
            'goodsRecposRes' => $goodsRecposRes,
        ]);
        return view();
    }
    public function edit()
    {

        $attrRes = db('attr')->where(array('type_id' => 3))->select();

        if (request()->isPost()) {

            $data = input('post.');

            //验证
            $validate = validate('goods');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $update = model('goods')->update($data);

            if ($update) {

                $this->success('修改商品成功 ！', 'lst');
            } else {

                $this->error('修改商品失败');
            }
            return;
        }
        //会员级别数据
        $mlRes = db('member_level')->field('level_name,id')->select();
        //获取类型
        $typeRes = db('type')->select();
        //品牌分类
        $brandRes = db('brand')->field('id,brand_name')->select();
        //会员价格
        $_mpRes = db('member_price')->where('goods_id', '=', input('id'))->select();
        //商品推荐位
        $goodsRecposRes = db('recpos')->where('rec_type', '=', 1)->select();
        //更改商品推荐位
        $_mygoodsRecposRes = db('rec_item')->where(array('value_type' => 1, 'value_id' => input('id')))->select();
        //改写二维数组为一维数组
        $mygoodsRecposRes = array();
        foreach ($_mygoodsRecposRes as $k => $v) {
            $mygoodsRecposRes[] = $v['recpos_id'];
        }

        $mpRes = array();

        foreach ($_mpRes as $k => $v) {
            $mpRes[$v['mlevel_id']] = $v;
        }
        //商品相册
        $gphotoRes = db('goods_photo')->where('goods_id', '=', input('id'))->select();

        // dump($mpRes);die;
        //查询当前商品基本信息
        $goods = db('goods')->find(input('id'));
        //查询当前商品属性信息
        $attrRes = db('attr')->where('type_id', '=', $goods['type_id'])->select();
        //查询当前商品拥有的商品属性
        $_gattrRes = db('goods_attr')->where('goods_id', '=', input('id'))->select();
        //更改二维数组结构为三维数组
        $gattrRes = array();
        foreach ($_gattrRes as $k => $v) {
            $gattrRes[$v['attr_id']][] = $v;
        }

        //商品分类
        $category    = new Catetree();
        $categoryObj = db('category');
        $categoryRes = $categoryObj->order('sort ACS')->select();
        $categoryRes = $category->Catetree($categoryRes);
        $this->assign([
            'mlRes'            => $mlRes,
            'typeRes'          => $typeRes,
            'brandRes'         => $brandRes,
            'categoryRes'      => $categoryRes,
            'goods'            => $goods,
            'mpRes'            => $mpRes,
            'gphotoRes'        => $gphotoRes,
            'attrRes'          => $attrRes,
            'gattrRes'         => $gattrRes,
            'goodsRecposRes'   => $goodsRecposRes,
            'mygoodsRecposRes' => $mygoodsRecposRes,
        ]);
        return view();
    }
    public function del($id)
    {
        $del = model('goods')->destroy($id);
        if ($del) {

            $this->success('删除商品成功 ！', 'lst');
        } else {

            $this->error('删除商品失败');
        }
    }
    // 库存
    public function product($id)
    {
        if (request()->isPost()) {
            db('product')->where('goods_id', '=', $id)->delete();
            $data      = input('post.');
            $goodsAttr = $data['goods_attr'];
            $goodsNum  = $data['goods_num'];
            $product   = db('product');
            foreach ($goodsNum as $k => $v) {
                $strArr = array();
                foreach ($goodsAttr as $k1 => $v1) {
                    if (intval($v1[$k]) <= 0) {
                        continue 2;
                    }
                    $strArr[] = $v1[$k];
                }
                sort($strArr);
                $strArr = implode(',', $strArr);
                $product->insert([
                    'goods_id'     => $id,
                    'goods_number' => $v,
                    'goods_attr'   => $strArr,
                ]);
            }
            $this->success('添加库存成功！');
            return;
        }
        $_radioAttrRes = db('goods_attr')->alias('g')->field('g.id,g.attr_id,g.attr_value,a.attr_name')->join("attr a", 'g.attr_id=a.id')->where(array('g.goods_id' => $id, 'a.attr_type' => 1))->select();
        $radioAttrRes  = array();
        // 数组格式重组
        foreach ($_radioAttrRes as $k => $v) {
            $radioAttrRes[$v['attr_name']][] = $v;
        }
        // 获取商品的库存信息
        $goodsProRes = db('product')->where('goods_id', '=', $id)->select();

        $this->assign([
            'radioAttrRes' => $radioAttrRes,
            'goodsProRes'  => $goodsProRes,
        ]);
        return view();
    }

    // 异步获取指定属性下的属性
    public function ajaxGetAttr()
    {
        $typeId  = input('type_id');
        $attrRes = db('attr')->where(array('type_id' => $typeId))->select();

        return json_encode($attrRes);
    }

    // 异步删除图片相册
    public function ajaxdelpic($id)
    {
        $gphoto   = db('goods_photo');
        $gphotos  = $gphoto->find($id);
        $ogPhoto  = IMG_UPLOADS . $gphotos['og_photo'];
        $bigPhoto = IMG_UPLOADS . $gphotos['big_photo'];
        $midPhoto = IMG_UPLOADS . $gphotos['mid_photo'];
        $smPhoto  = IMG_UPLOADS . $gphotos['sm_photo'];
        @unlink($ogPhoto);
        @unlink($bigPhoto);
        @unlink($midPhoto);
        @unlink($smPhoto);
        $del = $gphoto->delete($id);
        if ($del) {
            echo 1;
        } else {
            echo 2;
        }

    }

}
